<template id="forgot_password">

</template>

<script>
	export default {
		name: 'forgot_password',
		components: {},
		data() {
			return {
				sendAuthCode: true,
				/*布尔值，通过v-show控制显示‘获取按钮’还是‘倒计时’ */
				auth_time: 0,
				/*倒计时 计数器*/
				phone: "", //手机号
				code: "", //验证码
				coachid: "",
			    apiurl: "http://yogahome.top/Scanning/pay/getWxOpenid",
			    openid: "",
			}
		},

		created() {
			var param = location.href.split("?");
			var paramarr = param[1].split("&");
			var code = paramarr[0].split("=")[1];
			var state = paramarr[1].split("=")[1].split("#")[0];
			console.log(code + ":" + state);

			let obj = {
				//请求的参数
				code,
				state,
			};
			let _this = this;
			let qs = require('qs');
			this.$axios.post(_this.apiurl,
					qs.stringify(obj), {
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						}
						//重写headers 请求头  qs.stringify(json对象) 把对象转化为字符串
					}
				).then(response => {
					console.log(response);
					if(response.data.code==1){
						sessionStorage.setItem("openid",response.data.data);
						_this.$router.push({
						path:'/',
						query:{}
						})
					}else{
						_this.$router.push({
						path:'/',
						query:{}
						})
					}
				})
				.catch(function(error) {
					console.log(error);
				});
		},
		methods: {
			model() {
				this.phone = "";

			},
			phhotoChange(e) {
				this.phone = e.target.value;
			},
			Code() {
				
				let re = /^1\d{10}$/;
				let _this = this;
				if(this.phone) {
					if(re.test(_this.phone)) {
						var phone = _this.phone;
						let obj = {
							phone
						};
						let qs = require('qs');
						_this.$axios.post("http://yogahome.top/yujia/user/captcha", qs.stringify(obj), {
							headers: {
								'Content-Type': 'application/x-www-form-urlencoded'
							},
						}).then(function(response) {
							_this.sendAuthCode = false;
							_this.auth_time = 60;
							var auth_timetimer = setInterval(() => {
								_this.auth_time--;
								if(_this.auth_time <= 0) {
									_this.sendAuthCode = true;
									clearInterval(auth_timetimer);
								}
							}, 1000);

						}).catch(function(error) {
							console.log(error);
						});

					} else {
						_this.sendAuthCode = true;
						alert('手机号格式不正确,请重新输入')
					}

				} else {
					_this.sendAuthCode = true;
					alert('请输入手机号码')
				}

			},
			
			test(){
				console.log(this.openid)
			},
			Landed() {
				let _this = this
				if(this.phone) {
					var phone = this.phone; //手机号
					var code = this.code; //验证码
					let obj = {
						phone,
						code,
						openid: this.openid,

					};

					if(this.phone == "") {

					} else {

					}

					let qs = require('qs');
					this.$axios.post("http://yogahome.top/yujia/user/binduser", qs.stringify(obj), {
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},

					}).then(function(response) {
						//	
						let code = response.data.code;
						if(code == -1) {
							alert("没有输入验证码");
						} else if(code == -2) {
							alert("请微信登陆后在进行此操作");
							_this.$router.push('/forgot_password');
						} else if(code == -3) {
							alert("该用户已经绑定了第三方登陆");
						} else if(code == 0) {
							alert("教练账号不合法!请联系管理员注册");
						} else if(code == 1) {
							alert("登陆成功");
							_this.$router.push('/');
						} else if(code == 2) {
							alert("验证码验证失败");
						}
					}).catch(function(error) {
						console.log(error);
					})
				} else {
					alert("请输入手机号码")
				}

			},
		},
	}
</script>

<style>
	@import "../../assets/css/forgot_password.css";
</style>